package com.mvc.security.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.mvc.framework.model.Auditable;
import com.mvc.framework.service.BaseService;
import com.mvc.security.model.Operation;
import com.mvc.security.model.RoleOperation;
import com.mvc.security.model.UserRole;
@Service
public class OperationManager extends BaseService<Operation, Integer>{

	public List<Operation> getAll(){
		Operation operation = new Operation();
		operation.setRecStatus(Auditable.REC_STATUS_VALID);
		return getHibernateTemplate().findByExample(operation);
	}

	public List<Operation> getOperationsByRoleId(Integer roleId){
		String sql = "SELECT  A from  " + Operation.class.getName() + " A,"
	        + RoleOperation.class.getName() + " D " + "WHERE A.recStatus = " + Auditable.REC_STATUS_VALID
	        + " AND D.recStatus = " + Auditable.REC_STATUS_VALID + " AND A.objectId = D.operationId AND D.roleId = " + roleId;
		return searchBySql(sql);
	}

	public List<Operation> getOperationsByUserId(Integer userId){
		String sql = "SELECT  A from  " + Operation.class.getName() + " A,"
	        + RoleOperation.class.getName() + " D " + "WHERE A.recStatus = " + Auditable.REC_STATUS_VALID
	        + " AND D.recStatus = " + Auditable.REC_STATUS_VALID 
	        + " AND A.objectId = D.operationId AND D.roleId in (select roleId from  " + UserRole.class.getName() 
	        + " C where C.recStatus = " + Auditable.REC_STATUS_VALID + " AND C.userId = " + userId + ")";
		return searchBySql(sql);
	}

}
